package com.xiaoyu.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;

import com.xiaoyu.common.security.resourceserver.annotation.PermissionAuthorizeAnno;
import com.xiaoyu.user.entity.SystemUser;
import com.xiaoyu.user.sentinel.block.UserBlock;
import com.xiaoyu.user.sentinel.fallback.UserFallback;
import com.xiaoyu.user.service.ISystemUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zgb   Email: 1021180632@qq.com
 * @since 2020/9/21 13:55
 */
@Slf4j
@RequestMapping("user")
@RestController
@AllArgsConstructor
public class UserController {

    private final ISystemUserService iSystemUserService;

    @RequestMapping(value = "/testSentinel", method = RequestMethod.GET)
    @SentinelResource(value = "/user/testSentinel", blockHandlerClass = UserBlock.class, blockHandler = "listBlock",
            fallbackClass = UserFallback.class, fallback = "listBack")
    public void testSentinel() throws Exception {
        throw new Exception("熔断");
        // return R.ok("yes");
    }

    @RequestMapping(value = "/testSentinel1", method = RequestMethod.GET)
    @SentinelResource(value = "/user/testSentinel1", blockHandlerClass = UserBlock.class, blockHandler = "listBlock",
            fallbackClass = UserFallback.class, fallback = "listBack")
    public String testSentinel1() throws Exception {
        return "success";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @PermissionAuthorizeAnno
    public List<SystemUser> list() {
        return iSystemUserService.list();
    }

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public SystemUser getById() {
        return iSystemUserService.getById(1);
    }


}
